﻿namespace EyeOpen.Linq
{
	using System;
	using System.Collections.Generic;

	public static class EnumerableHierarchicalExtension
	{
		public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> recursion)
		{
			foreach (var item in source)
			{
				yield return item;

				var recursionSequence = recursion(item);

				if (recursionSequence == null)
				{
					continue;
				}

				foreach (var itemRecurse in Traverse(recursionSequence, recursion))
				{
					yield return itemRecurse;
				}
			}
		}

		public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
		{
			foreach (var item in source)
			{
				action(item);
			}
		}
	}
}
